const app = getApp()
const http = require('../../utils/http');

Page({
    data: {
        delBtnWidth: 160,
        isHaveCar: true,
        data: [],
        icon: {
            list: '../images/icon/list.png',
            more: '../images/icon/more.png',
            shop: '../images/icon/shop.png',
            addLan: '../images/icon/addLan.png',
            addHui: '../images/icon/addHui.png',
        }
    },
    onLoad: function () {
        this.getCarList();
    },

    getCarList: function () {
        http._get('/mini/queryCar?openid=' + app._openid).then(res => {
            if (res.code == 200) {
                console.log(res);
                if (res.result.length <= 0) {
                    this.setData({
                        isHaveCar: false
                    });
                } else {
                    this.setData({
                        data: res.result,
                        isHaveCar: true
                    })
                }


            } else {
                wx.showToast({
                    title: res.message,
                    duration: 2000
                });
                setTimeout(function () {
                    wx.hideToast()
                }, 2000)
            }
        }).catch(e => {
            console.log(e)
        });
    },
    goPlateNumber: function () {
        wx.navigateTo({
            url: '../plateNumber/plateNumber'
        })
    },
    drawStart: function (e) {
        var touch = e.touches[0]
        for (var index in this.data.data) {
            var item = this.data.data[index]
            item.right = 0
        }
        this.setData({
            data: this.data.data,
            startX: touch.clientX,
        })

    },
    drawMove: function (e) {
        var touch = e.touches[0]
        var item = this.data.data[e.currentTarget.dataset.index]
        var disX = this.data.startX - touch.clientX

        if (disX >= 20) {
            if (disX > this.data.delBtnWidth) {
                disX = this.data.delBtnWidth
            }
            item.right = disX
            this.setData({
                data: this.data.data
            })
        } else {
            item.right = 0
            this.setData({
                data: this.data.data
            })
        }
    },
    drawEnd: function (e) {
        var item = this.data.data[e.currentTarget.dataset.index]
        if (item.right >= this.data.delBtnWidth / 2) {
            item.right = this.data.delBtnWidth
            this.setData({
                data: this.data.data,
            })
        } else {
            item.right = 0
            this.setData({
                data: this.data.data,
            })
        }
    },

    delItem: function (e) {
        console.log("del")
        http._get('/mini/deleteCar?id=' + e.currentTarget.dataset.index).then(res => {
            console.log(res);
            if (res.code == 200) {
                wx.showToast({
                    title: "操作成功",
                    icon: 'success',
                    duration: 1800
                });
                this.getCarList();
                setTimeout(function () {
                    wx.hideToast()
                }, 2000)

            } else {
                wx.showToast({
                    title: res.message,
                    icon:'none',
                    duration: 2000
                });
                setTimeout(function () {
                    wx.hideToast()
                }, 2000)
            }
        }).catch(e => {
            console.log(e)
        });


    }
})
